$(function() {
	var flag = false;
	if ($.cookie("keeppassword") == "true") {
		$("#keeppassword").attr("checked", true);
		$("#loginname").val($.cookie("loginname"));
		var pwlength = $.cookie("pwlength");
		var pw = '';
		for (var i = 0; i < pwlength; i++) {
			pw += ' ';
		}
		$("#passwd").val(pw);
		flag = true;
	}
	$("#passwd").bind("focus", function(){
		if(flag){
			$(this).val('');
		}
		flag = false;
	});
	
	$("#loginbt").bind("click", function() {
		var loginname = $("#loginname").val();
		if (loginname == '请输入手机号') {
			myalert('手机号不能为空');
			return false;
		}
		var loginnameResult = checkLoginname(loginname);
		if (!loginnameResult.result) {
			myalert(loginnameResult.msg);
			return false;
		}
		if(!flag){
			var passwd = $("#passwd").val();
			if (passwd == '' || passwd == '请输入密码') {
				myalert("密码不能为空");
				return false;
			}
		}
		loginFirst(function(result) {
			if (result.code == '1') {
				$("#key1").val(result.key1);
				loginSubmit(function(result1) {
					if (result1.code == '1') {
						if($("#keeppassword").prop("checked")){
							$.cookie("loginname", $("#loginname").val(), {
								expires : 365 * 2,
								path : '/',
								domain: result1.suffix
							});
							var pw = '';
							if(flag){
								pw = $.cookie('passwd');
							} else {
								pw = $.md5($("#passwd").val() + $("#key1").val());
							}
							$.cookie("passwd", pw, {
								expires : 365 * 2,
								path : '/',
								domain: result1.suffix
							});
							$.cookie("keeppassword", true, {
								expires : 365 * 2,
								path : '/',
								domain: result1.suffix
							});
							$.cookie("pwlength", $("#passwd").val().length, {
								expires : 365 * 2,
								path : '/',
								domain: result1.suffix
							});
						} else {
							$.cookie('loginname', null, {
								path : '/',
								domain : '.xinghaoshangcheng.com',
								expires : -1
							});
							$.cookie('passwd', null, {
								path : '/',
								domain : '.xinghaoshangcheng.com',
								expires : -1
							});
							$.cookie('pwlength', null, {
								path : '/',
								domain : '.xinghaoshangcheng.com',
								expires : -1
							});
							$.cookie('keeppassword', null, {
								path : '/',
								domain : '.xinghaoshangcheng.com',
								expires : -1
							});
						}
						$.cookie("token", result1.token, {
							expires : 365 * 2,
							path : '/',
							domain: result1.suffix
						});
						// 本地测试需要加上端口号，另外修改hosts文件例如本机192.168.6.18 abcd123.xinghaoshangcheng.com
						// location.href = result1.domain + ":8080" + appPath + "/center/center.htm?token=" + result1.token;
						location.href = result1.domain + "/center/center.htm?token=" + result1.token;
					} else {
						myalert(result1.msg);
					}
				})
			} else {
				myalert(result.msg);
			}
		})
	});
	function loginFirst(callback) {
		showLoading();
		$.ajax({
			type : "post",
			url : appPath + "/member/checkLoginname.html",
			data : {
				loginname : $("#loginname").val()
			},
			success : function(result) {
				hideLoading();
				callback(result);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
	        }
		});
	}
	function loginSubmit(callback) {
		showLoading();
		var passwd = '';
		if(flag){
			passwd = $.cookie('passwd');
		} else {
			passwd = $.md5($("#passwd").val() + $("#key1").val())
		}
		$.ajax({
			type : "post",
			url : appPath + "/member/loginsubmit.html",
			data : {
				loginname : $("#loginname").val(),
				passwd : passwd
			},
			success : function(result) {
				hideLoading();
				callback(result);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				hideLoading();
				myalert("系统异常，请稍后再试");
	        }
		});
	}
})
function toLogin() {
	location.href = appPath + "/qzone/login.htm";
}